<?php

namespace app\modules\html\controllers;

use app\controllers\BaseController;

/**
 * [首页]页面展示控制器
 */
class IndexController extends BaseController
{

    /**
     * 页面布局文件
     */
    public $layout = '@app/views/layouts/mainAjax';

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'rules' => [
                    [ // 必须登陆才能访问
                        'actions' => [
                            'clear-cache',
                            'index',
                            'login',
                            'register'
                        ],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [ // 无需登陆即可访问
                        'actions' => [
                            'register',
                            'login'
                        ],
                        'allow' => true,
                        'roles' => ['?']
                    ],
                ],
                'denyCallback' => function ($rule, $action) {

                    // 下面需要[actionMethod]属性 - 没有返回
                    if (!property_exists($action, 'actionMethod')) {
                        return true;
                    }

//                    // 默认登陆以后还可打开登录页面吧，不然将出现错误页面
//                    if ($action->actionMethod == 'actionLogin' ||
//                        $action->actionMethod == 'actionRegister') {
//                        echo $this->showError(400, '您已经登录', [
//                            'title' => '您已登录'
//                        ]);
//                    }
                    return true;
                }
            ]
        ];
    }

    /**
     * 首页页面
     * @return mixed
     */
    public function actionIndex()
    {

        return $this->render('index');
    }

    /**
     * 用户注册页面
     * @return mixed
     */
    public function actionRegister()
    {

        // 注册和登录得走单独的布局
        $this->layout = '@app/views/layouts/mainLogin';
        return $this->render('register');
    }

    /**
     * 用户登录页面
     * @return mixed
     */
    public function actionLogin()
    {

        // 注册和登录得走单独的布局
        $this->layout = '@app/views/layouts/mainLogin';
        return $this->render('login');
    }

    /**
     * 更新缓存页面
     */
    public function actionClearCache()
    {

        return $this->render('clear-cache');
    }
}
